/**
 * Created by MADinow on 26.10.2014.
 */

/* -----------------------------------------------------------
         POPUP UI MODULE
 ----------------------------------------------------------- */

define(['settings/variables', 'ui/madGenerator', 'ui/mainContent-ui', 'ui/pageDimmer-ui',
        'events/popup-events', 'jquery', 'jquery_ui'],
    function (Variables, Genny, MainContent, PageDimmer, PopupEvents) {

        var _loginID = "Login";
        var _welcomeID = "Welcome" ;
        var _registerID = "Registration" ;

        var _currentPopWnd = null ;

        /**
         * getter for current popup window
         * @returns current popup window
         * @private
         */
        function _getCurrentPopupWnd(){
            return _currentPopWnd ;
        }

        /**
         * creates a new empty popup
         * @param targetDom
         * @param popupID
         * @private
         */
        function _createEmptyPopup(popupID){

            var _popupID = popupID+"_popup" ;

            _currentPopWnd = Genny.createDiv(PageDimmer.getPageDimmer(), _popupID, 'container-absolute page popup-window', true);
            var closeWndIcon = Genny.createDiv(_currentPopWnd, '', 'ui-icon close-icon tooltip', true);
            closeWndIcon.attr('data-tooltip', 'close window');
            Genny.createHeading(_currentPopWnd, '', '', '', popupID, 'h3', true);

            MainContent.blurContentScreen();
            PageDimmer.blurScreen();

            _currentPopWnd.show();
            _currentPopWnd.animate({'marginTop': '5%' }, 350);

            PopupEvents.registerPopupSpecificEvents(_popupID);

            // close event still in here -> should be moved to popup-events
            // -> but still causes an error of undefined !
            $('.close-icon').bind({
                click: function(e){
                    e.stopPropagation();
                    _closePopup();
                }
            });

        }

        /**
         * opens a new popup window with specified ID
         * @param targetDom
         * @param popupID
         * @private
         */
        function _openPopup(popupID){
            _createEmptyPopup(popupID);
            _checkForPopupType(popupID);
        }

        /**
         * checks for the specified popup ID and creates
         * the corresponding content
         * @param popupID
         * @private
         */
        function _checkForPopupType(popupID){
            if(popupID === _loginID)
                _createLoginForm();
        }

        function _createLoginForm(){

            var text = 'You are currently not logged in! In order to view the content of this site a login is required.' ;
            Genny.createParagraph(_currentPopWnd, '', '', '', text, true);

            var form = Genny.createForm(_currentPopWnd, '', 'container-absolute auto-width-element', 'post', 'controllers/login.php', true);
            var table = Genny.createTable(form, '', 'container-absolute auto-width-element', true);

            var rowUsername = Genny.createTableRow(table, '', '', '', true);
            var tdUsername = Genny.createTableData(rowUsername, '', '', '', '', true);
            var tdInputUsername = Genny.createTextInput(tdUsername, 'username', '', '', '', 'enter your username ... ', true);

            var rowPassword = Genny.createTableRow(table, '', '', '', true);
            var tdPassword = Genny.createTableData(rowPassword, '', '', '', '', true);
            var tdInputPassword = Genny.createPasswordInput(tdPassword, 'password', '', '', '', 'enter your password ... ', true);

            var rowSubmit = Genny.createTableRow(table, '', '', '', true);
            var tdSubmit = Genny.createTableData(rowSubmit, '', '', '', '', true);
            var tdInputSubmit = Genny.createSubmitButton(tdSubmit, 'submit', '', '', 'login', true);

            /*  create a cancel button ?
            var rowCancel = Genny.createTableRow(table, '', '', '', true);
            var tdCancel = Genny.createTableData(rowCancel, '', '', '', '', true);
            var tdInputCancel = Genny.createDefaultButton(tdCancel, 'cancel', '', '', 'cancel', true);
            */
        }

        /**
         * closes the currently opened popup
         * @private
         */
        function _closePopup(){

            _currentPopWnd.animate({'marginTop': '-500px' }, 350);

            PageDimmer.clarifyScreen();
            MainContent.focusContentScreen();

            setTimeout(function(){
                _currentPopWnd.remove();
            }, 350);
        }


        /**
         * public interface
         */
        return {

            /* variables */
            loginID: _loginID,
            welcomeID: _welcomeID,
            registerID: _registerID,
            getCurrentPopupWnd: _getCurrentPopupWnd,

            /* functions */
            openPopup: _openPopup,
            closePopup: _closePopup

        }
    }
);